package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PositionStudentMapper;
import com.ruoyi.system.domain.PositionStudent;
import com.ruoyi.system.service.IPositionStudentService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MedicalStudent;
import com.ruoyi.system.mapper.MedicalStudentMapper;

/**
 * 警校考生职位选择信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-01
 */
@Service
public class PositionStudentServiceImpl implements IPositionStudentService 
{
    @Autowired
    private PositionStudentMapper positionStudentMapper;

    @Autowired
    private MedicalStudentMapper medicalStudentMapper;

    /**
     * 查询警校考生职位选择信息
     * 
     * @param id 警校考生职位选择信息主键
     * @return 警校考生职位选择信息
     */
    @Override
    public PositionStudent selectPositionStudentById(Long id)
    {
        return positionStudentMapper.selectPositionStudentById(id);
    }

    /**
     * 查询警校考生职位选择信息列表
     * 
     * @param positionStudent 警校考生职位选择信息
     * @return 警校考生职位选择信息
     */
    @Override
    public List<PositionStudent> selectPositionStudentList(PositionStudent positionStudent)
    {
        return positionStudentMapper.selectPositionStudentList(positionStudent);
    }

    /**
     * 新增警校考生职位选择信息
     * 
     * @param positionStudent 警校考生职位选择信息
     * @return 结果
     */
    @Override
    public int insertPositionStudent(PositionStudent positionStudent)
    {
        // 验证该考生是否已通过体检面试
        MedicalStudent medical = new MedicalStudent();
        if (StringUtils.isNotEmpty(positionStudent.getIdCard())) {
            medical.setIdCard(positionStudent.getIdCard());
        } else {
            medical.setRegistrationId(positionStudent.getRegistrationId());
        }
//        medical.setMedicalInterview("通过");
        List<MedicalStudent> medicalList = medicalStudentMapper.selectMedicalStudentList(medical);
        
        if (medicalList == null || medicalList.isEmpty()) {
            throw new ServiceException("该考生未通过体检面试，不能参加职位选择");
        }
        
        // 如果资格审查、体能测评、体检面试均通过，则确保最终结果为"通过"
        if ("通过".equals(positionStudent.getQualificationReview()) && 
            "通过".equals(positionStudent.getPhysicalTest()) && 
            "通过".equals(positionStudent.getMedicalInterview()) &&
            !"通过".equals(positionStudent.getResult())) {
            positionStudent.setResult("通过");
        }
        
        return positionStudentMapper.insertPositionStudent(positionStudent);
    }

    /**
     * 修改警校考生职位选择信息
     * 
     * @param positionStudent 警校考生职位选择信息
     * @return 结果
     */
    @Override
    public int updatePositionStudent(PositionStudent positionStudent)
    {
        // 如果资格审查、体能测评、体检面试均通过，则确保最终结果为"通过"
        if ("通过".equals(positionStudent.getQualificationReview()) && 
            "通过".equals(positionStudent.getPhysicalTest()) && 
            "通过".equals(positionStudent.getMedicalInterview()) &&
            !"通过".equals(positionStudent.getResult())) {
            positionStudent.setResult("通过");
        }
        
        return positionStudentMapper.updatePositionStudent(positionStudent);
    }

    /**
     * 批量删除警校考生职位选择信息
     * 
     * @param ids 需要删除的警校考生职位选择信息主键
     * @return 结果
     */
    @Override
    public int deletePositionStudentByIds(Long[] ids)
    {
        return positionStudentMapper.deletePositionStudentByIds(ids);
    }

    /**
     * 删除警校考生职位选择信息信息
     * 
     * @param id 警校考生职位选择信息主键
     * @return 结果
     */
    @Override
    public int deletePositionStudentById(Long id)
    {
        return positionStudentMapper.deletePositionStudentById(id);
    }
    
    /**
     * 导入警校考生职位选择信息
     * 
     * @param positionStudentList 警校考生职位选择信息列表
     * @param updateSupport 是否更新支持，如果已存在，是否更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importPositionStudent(List<PositionStudent> positionStudentList, boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(positionStudentList) || positionStudentList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PositionStudent positionStudent : positionStudentList)
        {
            try
            {
                // 验证是否存在这个学生
                PositionStudent existingStudent = new PositionStudent();
                if (StringUtils.isNotEmpty(positionStudent.getIdCard())) {
                    existingStudent.setIdCard(positionStudent.getIdCard());
                } else if (StringUtils.isNotEmpty(positionStudent.getRegistrationId())) {
                    existingStudent.setRegistrationId(positionStudent.getRegistrationId());
                } else {
                    failureNum++;
                    failureMsg.append("<br/>第 " + failureNum + " 条数据导入失败：身份证号和报名序号不能同时为空");
                    continue;
                }
                
                // 验证该考生是否已通过体检面试
                MedicalStudent medical = new MedicalStudent();
                if (StringUtils.isNotEmpty(positionStudent.getIdCard())) {
                    medical.setIdCard(positionStudent.getIdCard());
                } else {
                    medical.setRegistrationId(positionStudent.getRegistrationId());
                }
//                medical.setMedicalInterview("通过");
                List<MedicalStudent> medicalList = medicalStudentMapper.selectMedicalStudentList(medical);
                
                if (medicalList == null || medicalList.isEmpty()) {
                    failureNum++;
                    failureMsg.append("<br/>第 " + failureNum + " 条数据导入失败：" + positionStudent.getName() + " 未通过体检面试，不能参加职位选择");
                    continue;
                }
                
                // 如果资格审查、体能测评、体检面试均通过，则确保最终结果为"通过"
                if ("通过".equals(positionStudent.getQualificationReview()) && 
                    "通过".equals(positionStudent.getPhysicalTest()) && 
                    "通过".equals(positionStudent.getMedicalInterview()) &&
                    !"通过".equals(positionStudent.getResult())) {
                    positionStudent.setResult("通过");
                }
                
                List<PositionStudent> students = selectPositionStudentList(existingStudent);
                
                if (students != null && students.size() > 0 && updateSupport)
                {
                    positionStudent.setId(students.get(0).getId());
                    positionStudent.setUpdateBy(operName);
                    updatePositionStudent(positionStudent);
                    successNum++;
                    successMsg.append("<br/>第 " + successNum + " 条数据更新成功");
                }
                else if (students != null && students.size() > 0 && !updateSupport)
                {
                    failureNum++;
                    failureMsg.append("<br/>第 " + failureNum + " 条数据已存在：" + positionStudent.getName());
                }
                else
                {
                    positionStudent.setCreateBy(operName);
                    insertPositionStudent(positionStudent);
                    successNum++;
                    successMsg.append("<br/>第 " + successNum + " 条数据导入成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>第 " + failureNum + " 条数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
